// set function parseTime,formatTime to filter
export { parseTime, formatTime } from '@/utils'
export { formatDate } from '@/common/js/date'
import common from '@/api/common'
import provinces from '@/common/provinces'

// 发布状态
export function releaseStatus(val) {
  return common.status[val]
}

// 楼盘状态
export function buildingStatus(val) {
  return common.buildingStatus[val - 1]
}
// 顾问状态
export function adviserStatus(val) {
  return common.adviserStatus[val]
}

// 省份
export function provinceStatus(val) {
  let name = ''
  provinces.forEach(v => {
    if (v.code === val) {
      name = v.name
    }
  })
  return name
}
// 城市
export function cityFilter(val) {
  const arr = []
  val.forEach(v => {
    arr.push(v.name)
  })
  return arr.join(',')
}

function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/* 数字 格式化*/
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
// 状态
export function status(val) {
  const status = ['未发布', '已发布']
  return status[val]
}
// 日期处理
export function biddingEndTime(val) {
  // let nowDate = parseInt(_.now())
  // let nowDate1 = parseInt(new Date(val).getTime())
  // let dateDiff = nowDate1 - nowDate
  // let dayDiff =  Math.floor(dateDiff / (24 * 3600 * 1000))
  // if (new Date(val) < new Date()) {
  if (!val) return ''
  if (strFormatDate(val).toDateString() === new Date().toDateString()) {
    return '今天截止'
  } else {
    const date = val.substr(5, 5).split('-')
    return date[0] + '月' + date[1] + '日'
  }
}
export function biddingEndTime2(val) {
  if (!val) return ''
  const date1 = val.split(' ')
  const date = date1[0].split('-')
  const time = date1[1].substr(0, 5)
  return date[0] + '年' + date[1] + '月' + date[2] + '日' + ' ' + time
}
export function biddingEndTime3(val) {
  if (!val) return ''
  const date1 = parseTime(val).split(' ')
  const date = date1[0].split('-')
  const time = date1[1].substr(0, 5)
  return date[0] + '年' + date[1] + '月' + date[2] + '日' + ' ' + time
}
export function biddingEndTime1(val) {
  // 后一天的时间
  //  Math.floor((_.now() / 1000) + (24 * 3600))
  const nowDay = timestampToTime() + '23:59:59'
  const nextDay = Math.floor(strFormatDate(nowDay).getTime() / 1000)
  const valDay = Math.floor(strFormatDate(val).getTime() / 1000)
  if (strFormatDate(val) < new Date()) {
    return '已截止'
  } else if (valDay < nextDay) {
    return '当天截止'
  } else {
    const nowDate = parseInt(this._.now())
    const nowDate1 = parseInt(strFormatDate(val).getTime())
    const dateDiff = nowDate1 - nowDate
    const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)) === 0 ? 1 : Math.floor(dateDiff / (24 * 3600 * 1000))
    return '剩余 ' + dayDiff + ' 天'
  }
}
export function auditTime(val) {
  // let nowDate = parseInt(_.now())
  // let nowDate1 = parseInt(new Date(val).getTime())
  // let dateDiff = nowDate1 - nowDate
  // let dayDiff =  Math.floor(dateDiff / (24 * 3600 * 1000))
  if (strFormatDate(val).toDateString() === new Date().toDateString()) {
    return '今天'
  } else {
    const date = val.substr(5, 5).split('-')
    return date[0] + '月' + date[1] + '日'
  }
}

export function DateFilter(val) {
  const date = val.substr(0, 10).split('-')
  return date[0] + '/' + date[1] + '/' + date[2]
}
export function DateFilterTime(val) {
  if (!val) return
  const date = val.substr(0, 16)
  return date.replace(/-/g, '/')
}

function strFormatDate(str) {
  return new Date(Date.parse(str.replace(/-/g, '/')))
}
function timestampToTime(timestamp) {
  var date = new Date()
  if (timestamp) {
    date = new Date(timestamp * 1000)
  }
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  var D = date.getDate() + ' '
  // var h = date.getHours() + ':'
  // var m = date.getMinutes() + ':'
  // var s = date.getSeconds()
  return Y + M + D
}

function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}
